<?php
class CategoryController extends Controller{
    public $layout='//layouts/admin';
    public function actionIndex(){
        session_start();
        $this->render('index');
    }
    public function actionPreInsertCategory(){
        session_start();
        $modelcategory = new Category();
        $listcategory =$modelcategory->findAll('id<=6');
        //$listcategory =$modelcategory->findByAttributes(array('id'=>6));
//        echo '<pre>';
//        print_r($listcategory);
//        die;
        $this->render('insertcategory',array('listcategory'=>$listcategory));
    }
    public function actionInsertCategory(){
        session_start();
        isset($_POST['categoryname'])?$categoryname = $_POST['categoryname']:$categoryname =null;
        isset($_POST['status'])?$status=$_POST['status']:$status=null;
        isset($_POST['parent_id'])?$parent_id=$_POST['parent_id']:$parent_id=null;
        if($categoryname==!null){
            $modelcategorymain = new Category();
            $modelcategorymain->category_name = $categoryname;
            $modelcategorymain->parent_id = $parent_id;
            $modelcategorymain->status = $status;
            $modelcategorymain->save();
            Yii::app()->user->setFlash('contact','thêm vào bảng thành công');
            $this->redirect('index.php?r=admin/category/index');
        }
    }

    public function actionListCategory(){
        session_start();
//        echo '<pre>';
//        print_r($_SESSION);
//        die;
        if(isset($_SESSION['admin'])){
            $model = new Category();
            $listcategory = $model->findAll();
            Yii::app()->user->setFlash('contact','ban co quyen truy cap');
            $this->render('listcategory',array('listcategory'=>$listcategory));
        }else{
            Yii::app()->user->setFlash('contact','không được quyền truy cập');
            $this->redirect('index.php?r=admin/category/index');
        }

    }

    public function actionPreEdit(){
        session_start();
//        echo '<pre>';
//        print_r($_SESSION);
//        die;
        if(isset($_SESSION['admin']['lever'])==1){
            $id = $_GET['id'];
            $model = new Category();
            $item = $model->findByPk($id);
            $listid = $model->findAll();
            $_SESSION['category']['id']= $id;
            $this->render('editcategory',array('item'=>$item,'listid'=>$listid));
        }else{
            Yii::app()->user->setFlash('contact','không đc phép sửa đổi');
            $this->redirect('index.php?r=admin/category/index');
        }

    }
    public function actionEdit(){
        session_start();
        if(isset($_SESSION['admin'])){
            $id = $_SESSION['category']['id'];
            $modelcategory = new Category();
            $update = $modelcategory->FindByPk($id);
            $update->category_name = $_POST['category_name'];
            $update->parent_id = $_POST['parent_id'];
            $update->glyphicons = $_POST['glyphicons'];
            $update->update();
            $_SESSION['category']['id']==null;
            Yii::app()->user->setFlash('contact','sửa đổi thành công');
            $this->redirect('index.php?r=admin/category/listcategory');
        }else{
            Yii::app()->user->setFlash('contact','không đc phép sửa đổi');
            $this->redirect('index.php?r=admin/category/index');
        }

    }
    public function actionDelete(){
        session_start();
        if(isset($_SESSION['admin']['lever'])==1){
            $id = $_SESSION['category']['id'];
            $modelcategory = new Category();
            $update = $modelcategory->FindByPk($id);
            $update->delete();
            session_unset($_SESSION['category']['id']);
            Yii::app()->user->setFlash('contact','xóa thành công');
            $this->redirect('index.php?r=admin/category/listcategory');
        }else{
            Yii::app()->user->setFlash('contact','không đc phép xóa đổi');
            $this->redirect('index.php?r=admin/category/index');
        }

    }
}